<?php

namespace app\api\controller;

use think\Controller;

class Channel extends Controller
{
    
    public function index(){
        header('Content-Type: text/html;charset=utf-8');
        header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
        header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
        header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段

        $spreader = input('SpreaderID',null);
        if(is_null($spreader)){
           puterror('channel empty');
        }

        $config = db('spreader_config','db_qpplatformdb')->where('SpreaderID','=',$spreader)->find();
    
        if($config){
            putsuccess([
                'url'=>$config['NoticeUrl'],
                'config'=>$config['Config'],
                'Control_Client_Show'=>$config['Control_Client_Show'],
                'Change_Pkg_Notice'=>$config['Change_Pkg_Notice']
            ]);
        }

        puterror('empty');       
    }

    public function test(){
        header('Content-Type: text/html;charset=utf-8');
        header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
        header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
        header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段
    
        echo 1;
    }

    public function user_device(){
        header('Content-Type: text/html;charset=utf-8');
        header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
        header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
        header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段
  
        $get = request()->get();
        //型号，牌子
        $user_device = db('user_device','db_operatelog')->where('user_id','=',$get['user_id'])->find();
        if($user_device){
            db('user_device','db_operatelog')->where('user_id','=',$get['user_id'])->update(
                [
                    'channel_id'=> empty($get['channel_id'])?'':$get['channel_id'],
                    'brand'=>empty($get['brand'])?'':$get['brand'],
                    'model'=>empty($get['model'])?'':$get['model'],
                    'manufacturer'=>empty($get['manufacturer'])?'':$get['manufacturer'],
                ]
            );
        }else{
            db('user_device','db_operatelog')->insert(
                [
                    'user_id'=>empty($get['user_id'])?'':$get['user_id'],
                    'channel_id'=>$get['channel_id'],
                    'add_time'=>date('Y-m-d H:i:s'),
                    'brand'=>empty($get['brand'])?'':$get['brand'],
                    'model'=>empty($get['model'])?'':$get['model'],
                    'manufacturer'=>empty($get['manufacturer'])?'':$get['manufacturer'],
                ]
            );
        }
        return 'success';
    }

        //渠道更新
    public function update_channel(){
        header('Content-Type: text/html;charset=utf-8');
        header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
        header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
        header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段
    
        $get = request()->get();
        if(!isset($get['default']) || !isset($get['channel_id'])){
            exit(json_encode(['code'=>0,'msg'=>'缺少参数','data'=>[]]));
        }
        $res = db('channel_update','db_operatelog')->where(['channel_id'=>$get['channel_id']])->find();
        if($res){
            exit(json_encode(['code'=>1,'msg'=>'','data'=>$res]));
        }
        //走默认
        $default = db('channel_update','db_operatelog')->where(['channel_id'=>$get['default']])->find();
        if(!$default){
            exit(json_encode(['code'=>-1,'msg'=>'未配置默认数据','data'=>[]]));
        }
        exit(json_encode(['code'=>1,'msg'=>'','data'=>$default]));
       


    }


    public function log(){
        $get = request()->get();
        if(!isset($get['user_id']) || !isset($get['channel_id'])){
            exit(json_encode(['code'=>0,'msg'=>'缺少参数','data'=>[]]));
        }
        $res = db('channel_log','db_operatelog')->where(['channel_id'=>$get['channel_id'],'user_id'=>$get['user_id'],'time'=>date('Y-m-d')])->find();
        if($res){
            exit(json_encode(['code'=>1,'msg'=>'','data'=>[]]));
        }
        db('channel_log','db_operatelog')->insert([
            'channel_id'=>$get['channel_id'],
            'user_id'=>$get['user_id'],
            'time'=>date('Y-m-d')
        ]);
        exit(json_encode(['code'=>1,'msg'=>'','data'=>[]]));

    }

}

